package com.dream.inspect.modules.business.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.dream.inspect.modules.dream.pojo.InstrucationsFormMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.toolkit.StringUtils;
import com.dream.inspect.common.utils.PageUtils;
import com.dream.inspect.common.utils.Query;
import com.dream.inspect.modules.business.common.CommonComponent;
import com.dream.inspect.modules.business.common.CommonUtil;
import com.dream.inspect.modules.business.dao.SuperviseStandardDao;
import com.dream.inspect.modules.business.dao.SuperviseTempletDao;
import com.dream.inspect.modules.business.entity.SuperviseStandardEntity;
import com.dream.inspect.modules.business.entity.SuperviseTempletEntity;
import com.dream.inspect.modules.business.service.SuperviseTempletService;
import com.dream.inspect.modules.sys.dao.SysDeptDao;
import com.dream.inspect.modules.sys.entity.SysDeptEntity;


@Service("superviseTempletService")
public class SuperviseTempletServiceImpl extends ServiceImpl<SuperviseTempletDao, SuperviseTempletEntity> implements SuperviseTempletService {
    @Autowired
    private SysDeptDao sysDeptDao;
    private ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
    @Autowired
    private SuperviseStandardDao superviseStandardDao;
    @Autowired
    private CommonComponent commonComponent;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        params.put("deptIds", commonComponent.findParentIds(Long.valueOf(params.get("deptId") + "")));
        Page<SuperviseTempletEntity> page = selectThisPage(new Query<SuperviseTempletEntity>(params),
                new Query<SuperviseTempletEntity>(params).getPage()
        );
        return new PageUtils(page);
    }

    private Page<SuperviseTempletEntity> selectThisPage(Map<String ,Object> map, Page<SuperviseTempletEntity> page) {
        page.setRecords(baseMapper.findTempleListByPage(map,page));
        return page;
    }
    @Override
    public List<SuperviseTempletEntity> findSuperviseTempletList(Map<String, String> params) {
        String projectId = params.get("projectId");
        int currPage = Integer.valueOf(params.get("currPage"));
        int pageSize = Integer.valueOf(params.get("pageSize"));
        currPage = (currPage - 1) * pageSize;
        List<SuperviseTempletEntity> stList = new ArrayList<SuperviseTempletEntity>();
        if (StringUtils.isNotEmpty(projectId)) {

            EntityWrapper<SuperviseTempletEntity> wrapper = new EntityWrapper<SuperviseTempletEntity>();
            wrapper.setEntity(new SuperviseTempletEntity());
            wrapper.eq("PROJECT_ID", projectId);
            stList = baseMapper.findSuperviseTempletList(wrapper, currPage, pageSize);
        }

        return stList;
    }

    @Override
    public int findSuperviseTempletListCount(Map<String, String> params) {
        String projectId = params.get("projectId");
        int countNum = 0;
        if (StringUtils.isNotEmpty(projectId)) {

            EntityWrapper<SuperviseTempletEntity> wrapper = new EntityWrapper<SuperviseTempletEntity>();
            wrapper.setEntity(new SuperviseTempletEntity());
            wrapper.eq("PROJECT_ID", projectId);
            countNum = baseMapper.findSuperviseTempletListCount(wrapper);
        }
        return countNum;
    }

    @Override
    public void addSuperviseTarget(SuperviseTempletEntity superviseTempletEntity, long deptId) {
        SysDeptEntity sysDeptEntity = sysDeptDao.selectById(deptId);
        String modeLevel = CommonUtil.getModeLevel(sysDeptEntity);
        superviseTempletEntity.setDeptId(deptId);
        superviseTempletEntity.setQh(sysDeptEntity.getQhId() + "");
        superviseTempletEntity.setShijdm(sysDeptEntity.getName());
        superviseTempletEntity.setLevel(modeLevel);
        insert(superviseTempletEntity);
       /* if ("0".equals(superviseTempletEntity.getMblx())) {
            //生成标准模板子级标准模板生成
            cachedThreadPool.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println("生成标准模板子级标准模板生成");
                }
            });
        }*/
     /*   if ("0".equals(superviseTempletEntity.getMsgFlag())) {
            //发生短信通知
            cachedThreadPool.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println("发送短信通知");
                }
            });
        }*/
    }

    @Override
    public List<SuperviseTempletEntity> findAllModelList(Map<String, Object> params) {
        List<SuperviseTempletEntity> templetEntityList = baseMapper.findAllTempleList();
        List<SuperviseTempletEntity> templetList = new ArrayList<SuperviseTempletEntity>();
        for (SuperviseTempletEntity superviseTempletEntity : templetEntityList) {
            List<SuperviseStandardEntity> list = findStandardListByIds(superviseTempletEntity.getMbwtx());
            superviseTempletEntity.setStandardList(list);
            templetList.add(superviseTempletEntity);
        }
        return templetList;
    }

    @Override
    public List<SuperviseTempletEntity> findAllModelToUserIdList(Map<String, Object> params) {
        //String deptIds="0";
        params.put("deptIds", commonComponent.findParentIds(Long.valueOf(params.get("deptId") + "")));
        List<SuperviseTempletEntity> templetEntityList = baseMapper.findSuperviseTempletListToDeptIds(params);
        return templetEntityList;
    }

    @Override
    public List<SuperviseStandardEntity> findSuperviseStandardList(Map<String, Object> params) {
        SuperviseTempletEntity superviseTempletEntity = selectById(Long.valueOf(params.get("id") + ""));
        List<SuperviseStandardEntity> list = findStandardListByIds(superviseTempletEntity.getMbwtx());
        return list;
    }


    /**
     * 根据id集获取问题
     *
     * @param ids
     * @return
     */
    public List<SuperviseStandardEntity> findStandardListByIds(String ids) {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("ids", ids);
        List<SuperviseStandardEntity> list = superviseStandardDao.findListByIds(param);
        return list;
    }

    @Override
    public List<SuperviseStandardEntity> findModelStandardListTree(Map<String, Object> params) {
        SuperviseTempletEntity superviseTempletEntity = selectById(Long.valueOf(params.get("id") + ""));
        // List<SuperviseStandardEntity> list = findStandardListTreeByIds(superviseTempletEntity.getMbwtx());
        List<SuperviseStandardEntity> list = findStandardListByIds(superviseTempletEntity.getMbwtx());
        Map<Long, Object> map = new HashMap<Long, Object>();
        for (SuperviseStandardEntity t : list) {
            map.put(t.getId(), t);
        }
        List<SuperviseStandardEntity> result = new ArrayList<SuperviseStandardEntity>();
        for (int i = 0; i < list.size(); i++) {
            SuperviseStandardEntity temp2 = list.get(i);
            if (null != map.get(temp2.getParentId())) {
                (((SuperviseStandardEntity) map.get(temp2.getParentId())).getChildren()).add(temp2);
                continue;
            } else {
                result.add(temp2);
            }
        }
        return result;
    }

    @Override
    public Integer updateStatus(SuperviseTempletEntity superviseTempletEntity) {
        Map<String, Object> params=new HashMap<>();
        params.put("dept_id",superviseTempletEntity.getDeptId());
        params.put("mblx","3");
        params.put("status","1");
        List<SuperviseTempletEntity> list =   baseMapper.selectByMap(params);
        if(list!=null&&list.size()>0){
            return 1;
        }
        superviseTempletEntity.setStatus("1");
        baseMapper.updateById(superviseTempletEntity);
        return 0;
    }

    @Override
    public Integer updateStatusConfirm(SuperviseTempletEntity superviseTempletEntity) {
        Map<String, Object> params=new HashMap<>();
        params.put("dept_id",superviseTempletEntity.getDeptId());
        params.put("mblx","3");
        params.put("status","1");
        List<SuperviseTempletEntity> list =   baseMapper.selectByMap(params);
        if(list!=null&&list.size()>0){
            for (SuperviseTempletEntity templetEntity : list) {
                templetEntity.setStatus("0");
                baseMapper.updateById(templetEntity);
            }
        }
        superviseTempletEntity.setStatus("1");
        baseMapper.updateById(superviseTempletEntity);
        return 0;
    }

    /**
     * 根据id集获取问题树
     *
     * @param ids
     * @return
     */
    public List<SuperviseStandardEntity> findStandardListTreeByIds(String ids) {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("ids", ids);
        param.put("levelNum", 1);

        List<SuperviseStandardEntity> list = superviseStandardDao.findListByIds(param);
        List<SuperviseStandardEntity> standardEntityList = new ArrayList<SuperviseStandardEntity>();
        for (SuperviseStandardEntity standardEntity : list) {
            findStandardListByIdTree(standardEntity, ids);
            standardEntityList.add(standardEntity);
        }
        return standardEntityList;
    }

    /**
     * 递归获取
     *
     * @param standardEntity
     * @param ids
     */
    public void findStandardListByIdTree(SuperviseStandardEntity standardEntity, String ids) {
        if (CommonUtil.LEVEL_4.equals(standardEntity.getLevelNum())) {
            return;
        }
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("ids", ids);
        param.put("code", standardEntity.getSbdm());
        param.put("levelNum", (Long.valueOf(standardEntity.getLevelNum()) + 1));

        List<SuperviseStandardEntity> list = superviseStandardDao.findListByIds(param);
        if(list!=null&&list.size()>0){
            List<SuperviseStandardEntity> standardEntityList = new ArrayList<SuperviseStandardEntity>();
            for (SuperviseStandardEntity entity : list) {
                findStandardListByIdTree(entity, ids);
                standardEntityList.add(entity);
            }
            standardEntity.setChildren(standardEntityList);
        }

    }

}
